package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dto.ReportUserStatisticsDTO;
import com.sky.mapper.*;
import com.sky.service.WorksPaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangkexuan
 * @className WorksPaceServiceImpl
 * @date 2023/6/16 20:51
 * @description
 */
@Service
public class WorksPaceServiceImpl implements WorksPaceService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {

        //获取新增用户数
        List<ReportUserStatisticsDTO> list = reportMapper.userStatistics(begin,end);
        int sum = list.stream().mapToInt(ReportUserStatisticsDTO::getNewUser).sum();

        //订单完成率
        OrderReportVO orderReportVO = reportMapper.ordersStatistics(begin,end);
        Double orderCompletionRate = (double)orderReportVO.getValidOrderCount() / orderReportVO.getTotalOrderCount();
        //有效订单数
        Integer validOrderCount = orderReportVO.getValidOrderCount();

        //营业额
        List<ReportUserStatisticsDTO> list2 = reportMapper.turnoverStatistics(begin,end);
        Double turnover = list2.stream().mapToDouble(ReportUserStatisticsDTO::getNewUser).sum();

        //平均客单价
        Double unitPrice = turnover/validOrderCount;

        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setNewUsers(sum).setOrderCompletionRate(orderCompletionRate)
                .setTurnover(turnover).setUnitPrice(unitPrice).setValidOrderCount(validOrderCount);
        return businessDataVO;
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        OrderStatisticsVO order = orderMapper.selectStatistics(begin, end);

        return OrderOverViewVO.builder()
                .waitingOrders(order.getToBeConfirmed())
                .deliveredOrders(order.getDeliveryInProgress())
                .completedOrders(order.getCompletedOrder())
                .cancelledOrders(order.getCancelledOrder())
                .allOrders(order.getAllOrder())
                .build();
    }
}
